const modules = import.meta.glob('@/pages/**')

export function guideLevel3Routes(params) {
  const asyncPaths = {}
  asyncRoutes.forEach(item => {
    const paths = item.path.split('/')
    const key = `/${paths[1]}/${paths[2]}`
    if (key in asyncPaths) {
      asyncPaths[key].push(item)
    } else {
      asyncPaths[key] = [item]
    }
  })
  return asyncPaths
}
/**
 * @description 格式化路由数据
 * @param {Array} value
 * @returns {Boolean}
 */
export function guidePermission(value, asyncPaths) {
  let obj = {}
  value.forEach((item, index) => {
    // 一级路由
    if (item.menuLevel === 1) {
      if (obj.hasOwnProperty(item.storeMenuId)) {
        obj[item.storeMenuId] = {
          menuUrl: item.storeMenuId,
          // menuUrl: item.menuUrl,
          menuName: item.menuName,
          menuLevel: item.menuLevel,
          sorted: item.sorted,
          storeMenuId: item.storeMenuId,
          menuIcon: item.menuIcon,
          children: obj[item.storeMenuId].children
        }
      } else {
        obj[item.storeMenuId] = {
          menuUrl: item.menuName === "首页" ? item.menuUrl : item.storeMenuId,
          // menuUrl: item.menuUrl,
          menuName: item.menuName,
          menuLevel: item.menuLevel,
          sorted: item.sorted,
          storeMenuId: item.storeMenuId,
          menuIcon: item.menuIcon,
          children: []
        }
      }
    } else if (item.menuLevel === 2) {
      // 当前项目是否包含路由对应的页面

      if (!modules.hasOwnProperty(`/src/pages${item.menuUrl}/index.jsx`)) return false
      // 二级路由
      if (obj.hasOwnProperty(item.menuParentId)) {
        obj[item.menuParentId] = {
          ...obj[item.menuParentId],
          children: [...obj[item.menuParentId].children, item]
        }
      } else {
        obj[item.menuParentId] = {
          children: [item]
        }
      }
    }

  })
  let routes = Object.values(obj).filter(item => {
    if (item.children.length > 0) {
      item.children = item.children.sort((a, b) => {
        return a.sorted - b.sorted
      })
      return item
    }

  }).sort((a, b) => {
    return a.sorted - b.sorted
  })
  return routes
}
// function getLevel3Router(path1, path2) {
//   const reg = new RegExp('^\/' + path1 + '\/' + path2 + '\/')
//   const result = asyncRoutes.filter(item => {
//     if (reg.test(item.path)) {
//       item.path = item.path.split('/')[3]
//       return item
//     }
//   })
//   return result.length > 0 ? result : []
// }
//对路由的component解析
function routerCom(path) {
  return null;

  return (resolve) => require([`@/views/${path}`], resolve);
}
